Skip to content

Use of Setters and Getters #7985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Use of Setters and Getters #7985

wants to merge 1 commit into from

Conversation

martin05
Copy link
Contributor

@martin05 martin05 commented Jun 2, 2017

If I have already define getter for the $targetDir, I should use it.

If I have already define getter for the $targetDir, I should use it.
@javiereguiluz
Copy link
Member

I always have doubts about what to do in these situations: accessing the property directly is faster ... but using the getter is safer because it may contain some logic besides the usual return $this->... I guess it's better so be safer 😄

@robfrawley
Copy link
Contributor

Generally, I would say that "getters are for the public API and direct property access is for within the class scope." Unless you have logic required, there is no benefit to calling the getter internally, and definitely some overhead for doing so.

@javiereguiluz
Copy link
Member

@robfrawley yes ... but what if my getter initially doesn't have logic ... and I access the property directly ... but later on I add login in the getter and forget to change the direct property access by the getter call?

@robfrawley
Copy link
Contributor

robfrawley commented Jun 4, 2017

Following that same logic, what about the inverse: if the value of the getter is formatted differently due to the expectations of your API while the use internally should be direct without the additional logic. Do you always want the logic of the getter to be consumed internally? No. Could you forget you've used it internally after the external requirements change. Yup.

I always directly consume properties unless I require logic, in which case I will only use the getter when it's logic aligns with what I expect internally, otherwise a special method dedicated to internal handling may be required.

Regardless, the burden of course always lies with the programmer to know what he/she's doing and properly update all usage instances after refactoring anything. This requirement remains regardless of how you are consuming the data.

@weaverryan
Copy link
Member

Regardless, the burden of course always lies with the programmer to know what he/she's doing and properly update all usage instances after refactoring anything.

Indeed :). So, it's not right or wrong either way. Personally, I do call the getter, even internally, though all the reasons listed here are valid.

👍

@weaverryan
Copy link
Member

Thanks @martin05!

@weaverryan weaverryan closed this Jul 6, 2017
weaverryan added a commit that referenced this pull request Jul 6, 2017
This PR was submitted for the 3.3 branch but it was merged into the 2.7 branch instead (closes #7985).

Discussion
----------

Use of Setters and Getters

If I have already define getter for the $targetDir, I should use it.

Commits
-------

789ed43 Use of Setters and Getters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants